package com.trainingcloud.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.trainingcloud.exception.DAOException;

public class HistoryDAO extends BaseDAO
{
	public Date getLatestUsedTime(int userId, int documentId) throws DAOException
	{
		Connection connection = getConnection();
		try 
		{
			PreparedStatement query = connection.prepareStatement("SELECT * FROM t_used_history WHERE user_id=? AND document_id=?");
			query.setInt(1, userId);
			query.setInt(2, documentId);
			ResultSet rs = query.executeQuery();
			if(rs.next())
			{
				return new Date(rs.getTimestamp("used_time").getTime());
			}
			else
			{
				return null;
			}
		}
		catch(SQLException e)
		{
			throw new DAOException(e);
		}
		finally
		{
			try
			{
				connection.close();
			}
			catch (SQLException e) 
			{
				throw new DAOException(e);
			}
		}
	}
	
	public void logHistory(int userId, int documentId, Date usedTime) throws DAOException
	{
		Connection connection = getConnection();
		try 
		{
			PreparedStatement query = connection.prepareStatement("SELECT * FROM t_used_history WHERE user_id=? AND document_id=?");
			query.setInt(1, userId);
			query.setInt(2, documentId);
			ResultSet rs = query.executeQuery();
			if(rs.next())
			{
				query = connection.prepareStatement("UPDATE t_used_history SET used_time=? WHERE user_id=? AND document_id=?");
				query.setTimestamp(1, new java.sql.Timestamp(usedTime.getTime()));
				query.setInt(2, userId);
				query.setInt(3, documentId);
				if(!query.execute() && query.getUpdateCount()==1)
				{
					// Success
				}
				else
				{
					throw new DAOException("Update t_used_history failed.");
				}
			}
			else
			{
				query = connection.prepareStatement("INSERT INTO t_used_history (user_id, document_id, used_time) VALUES (?, ?, ?)");
				query.setInt(1, userId);
				query.setInt(2, documentId);
				query.setTimestamp(3, new java.sql.Timestamp(usedTime.getTime()));
				if(!query.execute() && query.getUpdateCount()==1)
				{
					// Success
				}
				else
				{
					throw new DAOException("Insert t_used_history failed.");
				}
			}
				
//			query = connection.prepareStatement("DELETE FROM t_history WHERE user_id=? AND userd_time>(SELECT min(used_time) FROM t_history WHERE user_id=? LIMIT 10)");
//			query.setInt(1, userId);
//			query.setInt(2, userId);
//			if(!query.execute())
//			{
//				throw new DAOException("Delete from t_history failed.");
//			}
		}
		catch(SQLException e)
		{
			throw new DAOException(e);
		}
		finally
		{
			try
			{
				connection.close();
			}
			catch (SQLException e) 
			{
				throw new DAOException(e);
			}
		}
	}
	
	public void clearHistory(int userId, int documentId) throws DAOException
	{
		Connection connection = getConnection();
		try 
		{
			PreparedStatement query = connection.prepareStatement("DELETE FROM t_favorite WHERE user_id=? AND document_id=?");
			query.setInt(1, userId);
			query.setInt(2, documentId);
			if(!query.execute())
			{
				throw new DAOException("Delete from t_favorite failed.");
			}
		}
		catch(SQLException e)
		{
			throw new DAOException(e);
		}
		finally
		{
			try
			{
				connection.close();
			}
			catch (SQLException e) 
			{
				throw new DAOException(e);
			}
		}
	}
}
